#include "CampaignModel.h"



CampaignModel::CampaignModel(QObject *parent) :
    AbstractD20Model(parent)
{
    addGroup(AttributeGroup,    QT_TR_NOOP("Attribute"));
    addGroup(SkillGroup,        QT_TR_NOOP("Skill"));
    addGroup(FeatGroup,         QT_TR_NOOP("Feat"));
    addGroup(SpecialGroup,      QT_TR_NOOP("Special Ability"));
    addGroup(BonusGroup,        QT_TR_NOOP("Bonus"));
    addGroup(BonusLevelGroup,   QT_TR_NOOP("Bonus Level"));
}




#if 0
QString CampaignModel::groupName(const int& group)
{
    switch (group)
    {
    case AttributeGroup:    return ATTRIBUTE_GROUP;
    case SkillGroup:        return SKILL_GROUP;
    case FeatGroup:         return FEAT_GROUP;
    case SpecialGroup:      return SPECIAL_GROUP;
    case BonusGroup:        return BONUS_GROUP;
    case BonusLevelGroup:   return BONUS_LEVEL_GROUP;
    }

    return QString();
}



int CampaignModel::group(const QString& name)
{
    if (name.toLower().endsWith(ATTRIBUTE_GROUP.toLower())) return AttributeGroup;
    if (name.toLower().endsWith(SKILL_GROUP.toLower()))     return SkillGroup;
    if (name.toLower().endsWith(FEAT_GROUP.toLower()))      return FeatGroup;
    if (name.toLower().endsWith(SPECIAL_GROUP.toLower()))   return SpecialGroup;
    if (name.toLower().endsWith(BONUS_GROUP.toLower()))     return BonusGroup;
    if (name.toLower().endsWith(BONUS_LEVEL_GROUP.toLower())) return BonusLevelGroup;

    return Group_UNKNOWN;
}


void CampaignModel::setRootItem(AbstractItem* root)
{
    ItemModel::setRootItem(root);
    if (!rootItem()) return;

}



QModelIndex CampaignModel::groupIndex(const int& group) const
{
    if (!rootItem()) return QModelIndex();
    if (rootItem()->name().isEmpty()) rootItem()->setData(AbstractItem::NameData, "New Campaign Setting");

    QModelIndex searchIndex = index(0, AbstractItem::IdData, QModelIndex());
    QModelIndexList foundIndexes = match(searchIndex,
                                         Qt::EditRole,
                                         QString("%1.%2").arg(rootItem()->name()).arg(groupName(group)),
                                         1,
                                         Qt::MatchExactly);

    if (!foundIndexes.isEmpty()) return foundIndexes.first();


    return QModelIndex();
}



int CampaignModel::addItem(const int& group, const QString& name)
{
    QModelIndex parent = groupIndex(group);
    if (!parent.isValid()) return -1;

    int row = rowCount(parent);
    if (!insertRow(row, parent)) return -1;

    QModelIndex idIndex = index(row, AbstractItem::IdData, parent);
    QModelIndex nameIndex = idIndex.sibling(idIndex.row(), AbstractItem::NameData);

    setData(nameIndex, name);
    return row;
}



QModelIndexList CampaignModel::itemIndexes(const int& group) const
{
    // this also checks root existence
    QModelIndex parent = groupIndex(group);
    if (!parent.isValid()) return QModelIndexList();

    QModelIndex search = index(0, AbstractItem::IdData, parent);
    return match(search,
                 Qt::EditRole,
                 QString("%1.%2").arg(rootItem()->name()).arg(groupName(group)),
                 -1,
                 Qt::MatchStartsWith);
}




Qt::ItemFlags CampaignModel::flags(const QModelIndex &index) const
{
    QModelIndex nameIndex = index.sibling(index.row(), AbstractItem::NameData);
    if (group(nameIndex.data().toString()) == Group_UNKNOWN) return ItemModel::flags(index);

    return QAbstractItemModel::flags(index);
}
#endif

